
package nc.impl.arap.hsbcebankpay.hsbcebankpayvo;

import java.util.Arrays;

import org.apache.commons.lang3.ArrayUtils;

import nc.bs.framework.common.NCLocator;
import nc.vo.pub.BusinessException;
import nc.vo.pub.CircularlyAccessibleValueObject;
import nc.vo.pub.SuperVO;
import nc.vo.pub.ISuperVO;
import nc.vo.pub.IVOMeta;
import nc.vo.pub.VOStatus;
import nc.vo.pubapp.pattern.model.meta.entity.bill.IBillMeta;
import nc.vo.trade.pub.HYBillVO;
import nc.vo.pubapp.pattern.model.entity.bill.AbstractBill;
import nccloud.bs.excel.IXChangeContext;
import nccloud.bs.excel.plugin.AbstractImportProceeWithContext;
import nc.vo.arap.hsbcebankpay.HsbcEbankPayVO;
import nc.vo.arap.hsbcebankpay.HsbcEbankPayDetailVO;
import nc.vo.arap.hsbcebankpay.AggHsbcEbankPayVO;
import nc.itf.arap.hsbcebankpay.hsbcebankpayvo.IHsbcEbankPayVOService;

public class AggHsbcEbankPayVOImportProcess extends AbstractImportProceeWithContext {
	
	@Override
	protected void processBillWithContext(Object vo, IXChangeContext changcontext) throws BusinessException {
		
		AggHsbcEbankPayVO targetVO = null;
  		targetVO = (AggHsbcEbankPayVO)vo;
  		if(targetVO.getParentVO() != null) {
			targetVO.getParentVO().setStatus(VOStatus.NEW);
		}
		if(targetVO instanceof AbstractBill) {
			IBillMeta billMeta = targetVO.getMetaData();
				if(billMeta != null && ArrayUtils.isNotEmpty(billMeta.getChildren())) {
	   				IVOMeta[] ivoMetas = billMeta.getChildren();
	   				for(IVOMeta ivoMeta : ivoMetas) {
	      				if (ivoMeta != null) {
	         			ISuperVO[] childrens = targetVO.getChildren(ivoMeta);
	         				if (ArrayUtils.isNotEmpty(childrens)) {
	            				Arrays.stream(childrens).forEach(child -> {
	               					if (child != null) {
	                  					child.setStatus(VOStatus.NEW);
	               					}
	            				});
	         				}
	      				}
	   				}
				}
		} else {
			CircularlyAccessibleValueObject[] allChildrenVOs = targetVO.getAllChildrenVO();
  			if(ArrayUtils.isNotEmpty(allChildrenVOs)) {
  				Arrays.stream(allChildrenVOs).forEach(childrenVO -> {
  					if(childrenVO != null) {
  						childrenVO.setStatus(VOStatus.NEW);
  					}
  				});
  			}	
		}		
  		getService().initDefaultData(targetVO.getParentVO());
		getService().saveAggHsbcEbankPayVO(targetVO);
	}
	
	private IHsbcEbankPayVOService getService() {
		return NCLocator.getInstance().lookup(IHsbcEbankPayVOService.class);
	}

}
